// CREATE TABLE `t_user` (
//     `id` int(11) NOT NULL AUTO_INCREMENT,
//     `real_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '真实姓名',
//     `nick_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '昵称',
//     `gendor` int(1) DEFAULT NULL COMMENT '性别',
//     `city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '所在城市',
//     `join_date` int(11) DEFAULT NULL COMMENT '注册时间',
//     `love_status` int(1) DEFAULT NULL COMMENT '恋爱状态 0 单身  1 恋爱中 2 结婚  3 离婚',
//     `job` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '工作',
//     `uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '用户id',
//     `user_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '用户名',
//     `pass_word` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '密码',
//     `home_town` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '家乡',
//     `imgs` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '照片',
//     `phone_num` int(11) DEFAULT NULL COMMENT '手机号',
//     `weichat` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '微信',
//     `avatar` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '头像',
//     `birthday` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '生日',
//     `desc` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '签名描述',
//     `age` int(11) DEFAULT NULL COMMENT '年龄',
//     PRIMARY KEY (`id`,`uid`) USING BTREE
//   ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


let { v4: uuidv4 } = require("uuid")
const knex = require("../dao")
let TNAME = "t_user"

class User {
    Add = (data, callback) => {
        data.uid = uuidv4()
        knex(TNAME).insert(data).then(res => {
            callback(undefined, { code: 200, msg: "添加成功", data: {} })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Update = (data, callback) => {
        let { uid } = data
        knex(TNAME).where({ uid }).update(data).then(res => {
            callback(undefined, { code: 200, msg: "更新成功", data: {} })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    List = ({ page, page_size }, callback) => {
        knex(TNAME).select().limit(page_size || 10).offset(page || 0).then(res => {
            callback(undefined, { code: 200, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Detail = ({ uid }, callback) => {
        knex(TNAME).select().where({ uid }).then(res => {
            callback(undefined, { code: 200, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }

    Login=({user_name,pass_word},callback)=>{
        knex(TNAME).select().where({user_name,pass_word}).then(res => {
            callback(undefined, { code: 200, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
}

module.exports = User